package com.industrial.premu.controller;

import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.premu.domain.PreMuCompilation;
import com.industrial.premu.service.IPreMuCompilationService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 考核汇编材料Controller
 * 
 * @author ZHOU_S
 * @date 2024-01-22
 */
@RestController
@RequestMapping("/compilation")
public class PreMuCompilationController extends BaseController
{
    @Autowired
    private IPreMuCompilationService preMuCompilationService;

    /**
     * 查询考核汇编材料列表
     */
    @RequiresPermissions("premu:compilation:list")
    @GetMapping("/list")
    public TableDataInfo list(PreMuCompilation preMuCompilation)
    {
        startPage();
        List<PreMuCompilation> list = preMuCompilationService.selectPreMuCompilationList(preMuCompilation);
        return getDataTable(list);
    }

    /**
     * 导出考核汇编材料列表
     */
    @RequiresPermissions("premu:compilation:export")
    @Log(title = "考核汇编材料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PreMuCompilation preMuCompilation)
    {
        List<PreMuCompilation> list = preMuCompilationService.selectPreMuCompilationList(preMuCompilation);
        ExcelUtil<PreMuCompilation> util = new ExcelUtil<PreMuCompilation>(PreMuCompilation.class);
        util.exportExcel(response, list, "考核汇编材料数据");
    }

    /**
     * 获取考核汇编材料详细信息
     */
    @RequiresPermissions("premu:compilation:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId)
    {
        return success(preMuCompilationService.selectPreMuCompilationByBaseId(baseId));
    }

    /**
     * 新增考核汇编材料
     */
    @RequiresPermissions("premu:compilation:add")
    @Log(title = "考核汇编材料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PreMuCompilation preMuCompilation)
    {
        return toAjax(preMuCompilationService.insertPreMuCompilation(preMuCompilation));
    }

    /**
     * 修改考核汇编材料
     */
    @RequiresPermissions("premu:compilation:edit")
    @Log(title = "考核汇编材料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PreMuCompilation preMuCompilation)
    {

        LoginUser loginUser = SecurityUtils.getLoginUser();
        Set<String> roles= loginUser.getRoles();

        if(preMuCompilation.getCreator().contains(loginUser.getUserid())||roles.contains("p_01_011")){
            return toAjax(preMuCompilationService.updatePreMuCompilation(preMuCompilation));
        }else{
            return warn("无此权限修改！");
        }
    }

    /**
     * 删除考核汇编材料
     */
    @RequiresPermissions("premu:compilation:remove")
    @Log(title = "考核汇编材料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds)
    {
        LoginUser loginUser = SecurityUtils.getLoginUser();
       PreMuCompilation preMuCompilation=  preMuCompilationService.selectPreMuCompilationByBaseId(baseIds[0]);
        if(preMuCompilation.getCreator().contains(loginUser.getUserid())){
            return toAjax(preMuCompilationService.deletePreMuCompilationByBaseIds(baseIds));
        }else {
            return warn("无此权限删除！");
        }

    }
}
